package com.lemon.springboot.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liuli
 * @Date 2020/10/14 22:55
 */
public class DateUtil {

    public static final String parent = "yyyy-MM-dd hh:mm";

    public final static long _30SEC = 30*1000;

    public final static long _30MINI = 30*60*1000;

    private static Map<String, SimpleDateFormat> formatMap = new HashMap<>();

    public static String get30SecGranByTime(long time) {
        return dateFormat(parent).format(new Date(time - time%_30SEC));
    }

    public static String dateFormat(Date date, String pattern) {
        return dateFormat(pattern).format(date);
    }

    /**
     * 根据pattern创建SimpleDateFormat对象
     */
    public static final DateFormat dateFormat(final String pattern) {
        SimpleDateFormat simpleDateFormat = formatMap.get(pattern);
        if (simpleDateFormat == null) {
            synchronized (DateUtil.class) {
                if (simpleDateFormat == null) {
                    formatMap.put(pattern, new SimpleDateFormat(pattern));
                    simpleDateFormat = formatMap.get(pattern);
                }
            }
        }
        return simpleDateFormat;
    }
}
